/* entity */
import Field from '@model/entity/Field';
/* model */
import Column from '@model/types/Column';
import { selectColumnMaxMap, selectColumnNotSupportedFields } from '@src/modules/projectManage/projectManageList/model';
/* enum */
import { CommonMappingEnum, FieldTypeMappingEnum } from '@model/enum/FieldMappingEnum';
/* service */
import { getFieldName, isSearchField, isNotSupportedDisplayField } from '@service/FieldService';
/* hooks */
import { useStorage, StorageModuleEnum, StorageSetType } from '@hooks/use-storage';
/* util */
import formatColumn from 'packages/BaseTableCell/formatColumn';
import { isUndefined } from '@src/util/type';
// @ts-ignore
import { getUserId } from '@src/util/storage.ts';
import { Message } from 'element-ui';
import i18n from '@src/locales';
import _ from 'lodash';

// @ts-ignore
const storage = useStorage('shb-pc-fe', StorageModuleEnum.ProjectManage, getUserId());

// 保存数据到缓存
function saveDataToStorage(key: string, data: StorageSetType) {
  storage.setItem(key, data)
}

// 从缓存获取数据
async function getDataToStorage(key: string, data?: any) {
  return await storage.getItem(key, data);
}

// 高级搜索字段
function getAdvancedFields(fields: Field[]) {
  let dvancedFields = fields.map((field: Field) => {
    // 产品
    if (getFieldName(field) === CommonMappingEnum.Product) field.formType = FieldTypeMappingEnum.Text;

    return field;
  })
  
  return dvancedFields.filter((field: Field) => isSearchField(field));
}

// 选择列
function getColumnFields(fields: Field[], mode: string) {
  // 选择列个数限制
  const max = selectColumnMaxMap[mode];
  // 选择列不支持的字段
  const notSupportedFields = selectColumnNotSupportedFields[mode];

  const originColumns = fields
    .filter((field: Field) => !isNotSupportedDisplayField(field) && !notSupportedFields?.includes(field.fieldName))
    .map((field: Field) => {
      return {
        ...formatColumn(field),
        show: isUndefined(field.show) ? true : field.show,
      }
    })
    
    // 没有个数限制
    if (!max) return originColumns;

    let count = 0;

    return originColumns.reduce((pre, cur) => {
      if (cur.show) count++;

      // 超过限制 show置成false
      if (count > max) cur.show = false;
      
      pre.push(cur);

      return pre;
    }, []);
}

// 精简列属性
function simplifyTableColumnsProperty(columns: Column[]): Column[] {
  return (
    columns.map((column: Column) => ({
      field: column.field,
      show: column.show,
      width: column.width,
      fixLeft: column.fixLeft
    }))
  )
}

/* 设置项目列表字段的值 */
function setProjectPageList(data: any[]) {
  let list: any[] = [];

  data.forEach((item: any) => {
    list.push({
      ...item,
      ...item.attributeMap,
      attribute: item.attributeMap,
      customer: item.customer?.name,
      linkman: item.linkman?.name,
      projectType: item.templateName,
      managerPerson: item.managerList,
      synergies: item.synergiesList,
      projectProcess: item.projectProcess || 0,
      projectStatusColor: item.projectStatusColor || '#BDBDBD',
      product: (item.productVO || []).map((pro: any) => pro.name)?.join('，'),
    })
  });

  return list;
}

export {
  simplifyTableColumnsProperty,
  saveDataToStorage,
  getDataToStorage,
  getColumnFields,
  getAdvancedFields,
  setProjectPageList,
}
